// @ts-nocheck
import router from '@system.router';
import BaseParseConfModel from '../../model/baseParseConfImpl/BaseParseConfModel.js';
import LogUtil from '../../common/baseUtil/LogUtil.js';
import runningLock from '@ohos.runningLock';
import prompt from '@ohos.prompt';

let baseParseConfModel = new BaseParseConfModel();
let logUtil = new LogUtil();
let list = [
    {
        "settingIcon": "/res/image/icon_backGround.png",
        "settingType": 1,
        "settingAlias": "backGround",
        "settingTitle": "",
        "settingValue": "unknown",
        "settingArrow": "/res/image/ic_settings_arrow.png",
        "settingShowDivider": true
    },
    {
        "settingIcon": "/res/image/icon_backGround.png",
        "settingType": 1,
        "settingAlias": "proximity",
        "settingTitle": "",
        "settingValue": "unknown",
        "settingArrow": "/res/image/ic_settings_arrow.png",
        "settingShowDivider": true
    },
    {
        "settingIcon": "/res/image/icon_isSupported.png",
        "settingType": 1,
        "settingAlias": "isSupported",
        "settingTitle": "",
        "settingValue": "",
        "settingArrow": "/res/image/ic_settings_arrow.png",
        "settingShowDivider": true,
        "settingUri": "pages/isSupportedPage/isSupportedPage"
    }
]

export default {
    data: {
        runningLockList: [],
        backGroundLock: null,
        proximityLock: null,
        used: false,
        lockJsonPath: '/data/app/el1/bundle/public/com.example.powerhap/com.example.entry/assets/power/resources/rawfile/runninglock.json'
    },
    onInit() {
        logUtil.info('runningLockPage onInit  start:')
        this.runningLockList = baseParseConfModel.getJsonData(this.lockJsonPath);
        this.runningLockList = this.runningLockList || list
        logUtil.info('runningLockPage get runningLockList by json:' + JSON.stringify(this.runningLockList))
        for (let key in this.runningLockList) {
            var settingAlias = this.runningLockList[key].settingAlias
            this.runningLockList[key].settingTitle = this.$t('strings.'.concat(settingAlias));
        }
        logUtil.info('runningLockPage onInit end:')
    },
    showToast(error) {
        prompt.showToast({
            message: 'code: ' + error.code + ' msg: ' + error.message,
            duration: 3000,
        });
    },
    createBackGroundLock() {
        if (this.backGroundLock != null) {
            return;
        }
        try {
            runningLock.create("backGround", runningLock.RunningLockType.BACKGROUND,
                (error, lockIns) => {
                    if (typeof error !== 'undefined') {
                        this.showToast(error);
                        return;
                    }
                    logUtil.info('create success, isHolding: ' + lockIns.isHolding())
                    this.backGroundLock = lockIns;
                })
        } catch (error) {
            this.showToast(error);
        }
    },
    createProximityLcok() {
        if (this.proximityLock != null) {
            return;
        }
        try {
            runningLock.create("proximity", runningLock.RunningLockType.PROXIMITY_SCREEN_CONTROL)
                .then((lockIns) => {
                    logUtil.info('proximity create success, isHolding: ' + lockIns.isHolding())
                    this.proximityLock = lockIns;
                }).catch((error) => {
                this.showToast(error);
            })
        } catch(error) {
            this.showToast(error);
        }
    },
    onBgLock() {
        this.createBackGroundLock();
        if (this.backGroundLock == null) {
            return;
        }
        let used = this.backGroundLock.isHolding();
        logUtil.info('backGroundLock isHolding: ' + used)
        if (used) {
            logUtil.info('screenLock is isHolding:')
            this.backGroundLock.unhold();
            logUtil.info('backGroundLock unhold success:')
            this.runningLockList[0].settingValue = 'unlocked'
        } else {
            logUtil.info('backGroundLock is not Used:')
            this.backGroundLock.hold(60000);
            logUtil.info('backGroundLock hold success:')
            this.runningLockList[0].settingValue = 'locked 10 minutes'
        }
    },
    onProximityLcok() {
        this.createProximityLcok();
        if (this.proximityLock == null) {
            return;
        }
        let used = this.proximityLock.isHolding();
        logUtil.info('proximityLock used: ' + used)
        if (used) {
            logUtil.info('proximityLock is used:')
            this.proximityLock.unhold();
            logUtil.info('proximityLock unlock success:')
            this.runningLockList[1].settingValue = 'unlocked'
        } else {
            logUtil.info('proximityLock is not Used:')
            this.proximityLock.hold(60000);
            logUtil.info('proximityLock lock success:')
            this.runningLockList[1].settingValue = 'locked 10 minutes'
        }
    },
    onIsSupportedPage(event) {
        logUtil.info('isSupported onclick turn to : ' + JSON.stringify(event.detail.text));
        router.push({
            uri: event.detail.text.settingUri,
            params: {
                dataParam: event.detail.text
            }
        });
        logUtil.info('isSupported onclick turn end:');
    },
    onclickLock(event) {
        logUtil.info('runningLockListComponent onclick start: ' + JSON.stringify(event.detail.text.settingAlias))
        switch (event.detail.text.settingAlias) {
            case "backGround":
                this.onBgLock()
                break;
            case "proximity":
                this.onProximityLcok()
                break;
            case "isSupported":
                this.onIsSupportedPage(event)
                break;
            default:
                break;
        }
    },
    runningLockBack() {
        logUtil.info('runningLockPage back start:')
        router.back();
        logUtil.info('runningLockPage back end:')
    },
    onBackPress() {
        logUtil.info('runningLockPage onBackPress:')
        router.back();
    },
    onCreate() {
        logUtil.info('runningLockPage onCreate')
    },
    onReady() {
        logUtil.info('runningLockPage onReady')
    },
    onShow() {
        logUtil.info('runningLockPage onShow')
    },
    onHide() {
        logUtil.info('runningLockPage onHide')
    },
    onDestroy() {
        logUtil.info('runningLockPage onDestroy')
    }
}